package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.impl.dao.SupervisionRecordsDao;
import com.jiantong.work.model.SupervisionRecords;
import com.jiantong.work.service.SupervisionRecordsReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 督查管理-督查记录
 * Created by mz03 on 2017/3/24.
 *
 * @author :zyp
 */
@Slf4j
@Service
@RpcProvider
public class SupervisionRecordsReadServiceImpl implements SupervisionRecordsReadService {

    private final SupervisionRecordsDao supervisionRecordsDao;

    @Autowired
    public SupervisionRecordsReadServiceImpl(SupervisionRecordsDao supervisionRecordsDao) {
        this.supervisionRecordsDao = supervisionRecordsDao;
    }

    /**
     * id 查找
     *
     * @param id
     * @return Quotes
     */
    @Override
    public Response<SupervisionRecords> finById(Long id) {
        try {
            SupervisionRecords supervisionRecords = supervisionRecordsDao.findById(id);
            if (supervisionRecords == null) {
                log.error("supervisionRecords (id={}) is not find", id);
                return Response.fail("supervisionRecords is null so not.find");
            }
            return Response.ok(supervisionRecords);
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(id={}),cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("veterinaryDrugsReport.find.fail");
        }
    }

    /**
     * 查找全部分页查询  未确定添加条件
     */
    @Override
    public Response<Paging<SupervisionRecords>> findByAll(Integer pageNo, Integer pageSize) {
        try {
            Map<String, Object> map = Maps.newHashMap();
            LoginUser loginUser = UserUtil.getCurrentUser();
            List<Long> breedFactoryIds = loginUser.getBreedFactoryIds();
            if(!breedFactoryIds.isEmpty()) map.put("breedFactoryIds",breedFactoryIds);
            else return Response.ok(new Paging<>(0L,new ArrayList<>()));

            map.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Paging<SupervisionRecords> paging = supervisionRecordsDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), map);//,,,
            if (paging == null) {
                log.error("veterinaryDrugsReportList is not find");
                return Response.fail("veterinaryDrugsReportList is null so not find");
            }
            return Response.ok(paging);
        } catch (Exception e) {
            log.error("failed to find veterinaryDrugsReportList,cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("veterinaryDrugsReportList.find.fail");
        }
    }

    /**
     * 根据状态分页查找
     * 1,政府端登录人: 根据当前登录人管理的养殖场 显示这些养殖场中的所有督查记录.
     * 2,养殖端登录人: 拿到当前登录人所在的养殖场ID, 根据此id查询相关的督查记录.
     */
    @Override
    public Response<Paging<SupervisionRecords>> findByStatus(Integer pageNo, Integer pageSize, Integer status) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            List<Long> breedFactoryIds = new ArrayList<>();

            LoginUser loginUser = UserUtil.getCurrentUser();

            if (loginUser.getOrganizationType() == 1) {//如果是养殖端人员,拿到于此养殖场相关的督查记录
                breedFactoryIds.add(loginUser.getOrganizationId());
            }else if (loginUser.getOrganizationType() == 3){ //如果是政府端人员,查询此人管辖的所有养殖场的督查记录
                breedFactoryIds = loginUser.getBreedFactoryIds();
            }
            if(!breedFactoryIds.isEmpty()) criteria.put("breedFactoryIds", breedFactoryIds);
            else return Response.ok(new Paging<>(0L,new ArrayList<>()));
            if (status != null) {
                criteria.put("status", status);
            }
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(supervisionRecordsDao.pagingByStatus(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(status={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("supervisionRecords.find.fail");
        }

    }

    /**
     * 不分页
     * 根据状态查询当月所有人的督查记录 根据养殖场去重  用来显示督查轨迹地图上的养殖场(当前操作人员为管理人员时)
     *
     * @param status
     * @return Paging<SupervisionRecords>
     */
    @Override
    public Response<List<SupervisionRecords>> findByMonth(Integer status) {
        try {
            Map<String, Object> criteria = Maps.newHashMap();
            if (status != null) {
                criteria.put("status", status);
                criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            }
            return Response.ok(supervisionRecordsDao.findByMonth(criteria));
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(status={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("supervisionRecords.find.fail");
        }
    }

    /**
     * 不分页
     * 根据状态查询当月指定一个政府工作人员参与的督查记录,如果userId为null就查询全部 去重  用来显示督查轨迹地图上的养殖场(当前操作人员为工作人员时)
     *
     * @param status
     * @param userId
     * @return Paging<SupervisionRecords>
     */
    @Override
    public Response<List<SupervisionRecords>> findByMonthUserId(Integer status, Long userId, List<Long> breedFactoryIds) {
        try {
            Map<String, Object> criteria = Maps.newHashMap();
            if (status != null) {
                criteria.put("status", status);
//                criteria.put("userId", userId);
                criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            }
            if (breedFactoryIds != null && !breedFactoryIds.isEmpty()){
                criteria.put("breedFactoryIds",breedFactoryIds);
            }
            return Response.ok(supervisionRecordsDao.findByMonth(criteria));
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(status={} and userId = {}), cause:{}", status, userId, Throwables.getStackTraceAsString(e));
            return Response.fail("supervisionRecords.find.fail");
        }
    }

    /**
     * 分页 不去重
     * 只根据status查看全部轨迹
     *
     * @param pageNo
     * @param pageSize
     * @param status
     * @return Paging<SupervisionRecords>
     */
    @Override
    public Response<Paging<SupervisionRecords>> findByMonthStatus(Integer pageNo, Integer pageSize, Integer status) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if (status != null) {
                criteria.put("status", status);
                criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            }
            return Response.ok(supervisionRecordsDao.pagingFindByMonth(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(status={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("supervisionRecords.find.fail");
        }
    }

    /**
     * 分页 不去重
     * 根据status.userId查看督查轨迹
     *
     * @param pageNo
     * @param pageSize
     * @param status
     * @param userId
     * @return Paging<SupervisionRecords>
     */
    @Override
    public Response<Paging<SupervisionRecords>> findByMonthStatusAndUserId(Integer pageNo, Integer pageSize, Integer status, Long userId) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if (status != null) {
                criteria.put("status", status);
                criteria.put("userId", userId);
                criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            }
            return Response.ok(supervisionRecordsDao.pagingFindByMonth(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(status={} userId ={}), cause:{}", status, userId, Throwables.getStackTraceAsString(e));
            return Response.fail("supervisionRecords.find.fail");
        }
    }

    /**
     * 分页
     * 根据养殖场名字查询存在这个养殖场上的督查轨迹,并且是当月的,所有的督查记录.     用来点击地图上的点显示详情
     *
     * @param pageNo
     * @param pageSize
     * @param status
     * @param breedFactoryId
     * @return Paging<SupervisionRecords>
     */
    @Override
    public Response<Paging<SupervisionRecords>> findByMonthBreedName(Integer pageNo, Integer pageSize, Integer status, Long breedFactoryId) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if (status != null) {
                criteria.put("status", status);
                criteria.put("breedFactoryId", breedFactoryId);
                criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            }
            return Response.ok(supervisionRecordsDao.pagingFindByMonth(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(status={};breedFactoryId={}), cause:{}", status, breedFactoryId, Throwables.getStackTraceAsString(e));
            return Response.fail("supervisionRecords.find.fail");
        }
    }

    /**
     * 分页
     * 根据养殖场名字查询存在这个养殖场上的督查轨迹,并且是当月的,指定督查人的督查记录.     用来点击地图上的点显示详情
     *
     * @param pageNo
     * @param pageSize
     * @param status
     * @param breedFactoryId
     * @param userId
     * @return Paging<SupervisionRecords>
     */
    @Override
    public Response<Paging<SupervisionRecords>> findByMonthBreedNameUserId(Integer pageNo, Integer pageSize, Integer status, Long breedFactoryId, Long userId) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if (status != null) {
                criteria.put("status", status);
                criteria.put("breedFactoryId", breedFactoryId);
                criteria.put("userId", userId);
                criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            }
            return Response.ok(supervisionRecordsDao.pagingFindByMonth(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(status={};breedFactoryId={};userId={}), cause:{}", status, breedFactoryId, userId, Throwables.getStackTraceAsString(e));
            return Response.fail("supervisionRecords.find.fail");
        }
    }

    /**
     * 根据养殖场Id查询存在这个养殖场上的督查轨迹,并且是当月的,所有的督查记录.     用来组SupervisionRecordsDTO
     *
     * @param status
     * @param breedFactoryId
     * @param userId
     * @return List<SupervisionRecords>
     */
    @Override
    public Response<List<SupervisionRecords>> findByMonthBreedId(Integer status, Long breedFactoryId, Long userId) {
        try {
            Map<String, Object> criteria = Maps.newHashMap();
            if (status != null || breedFactoryId != null) {
                criteria.put("status", status);
                criteria.put("breedFactoryId", breedFactoryId);
                criteria.put("userId", userId);
                criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            }
            return Response.ok(supervisionRecordsDao.findByBreedFactoryId(criteria));
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(status={};breedFactoryId={};userId={}), cause:{}", status, breedFactoryId, userId, Throwables.getStackTraceAsString(e));
            return Response.fail("supervisionRecords.find.fail");
        }
    }

    @Override
    public Response<Paging<SupervisionRecords>> pagingCommon(Integer pageNo, Integer pageSize, Map criteria) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            return Response.ok(supervisionRecordsDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(status={};breedFactoryId={};userId={}), cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("supervisionRecords.find.fail");
        }
    }

    /**
     * 查询所有督察人员名单，去重
     * @return
     */
    @Override
    public Response<List<String>> findAllName(){
        try {
            return Response.ok(supervisionRecordsDao.findAllName());
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(status={};breedFactoryId={};userId={}), cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("supervisionRecords.find.fail");
        }
    }
    @Override
    public Response<Paging<SupervisionRecords>> pagingcommon(Integer pageNo, Integer pageSize, Map criteria){
        try {
            return Response.ok(supervisionRecordsDao.pagingcommon(pageNo, pageSize, criteria));
        } catch (Exception e) {
            log.error("failed to find supervisionRecords(status={};breedFactoryId={};userId={}), cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("supervisionRecords.find.fail");
        }
    }


}
